package com.fantabest.console.client.widget;

import com.extjs.gxt.ui.client.Registry;
import com.extjs.gxt.ui.client.Style.HorizontalAlignment;
import com.extjs.gxt.ui.client.data.BeanModel;
import com.extjs.gxt.ui.client.data.BeanModelFactory;
import com.extjs.gxt.ui.client.data.BeanModelLookup;
import com.extjs.gxt.ui.client.event.ButtonEvent;
import com.extjs.gxt.ui.client.event.SelectionListener;
import com.extjs.gxt.ui.client.mvc.Dispatcher;
import com.extjs.gxt.ui.client.widget.MessageBox;
import com.extjs.gxt.ui.client.widget.button.Button;
import com.extjs.gxt.ui.client.widget.form.FormButtonBinding;
import com.extjs.gxt.ui.client.widget.form.FormPanel;
import com.extjs.gxt.ui.client.widget.form.TextField;
import com.fantabest.common.client.AppEvents;
import com.fantabest.common.client.FantaBestConstants;
import com.fantabest.common.client.FantaBestMessages;
import com.fantabest.common.client.LoginServiceAsync;
import com.fantabest.common.client.RegistryKeys;
import com.fantabest.common.entity.Manager;
import com.google.gwt.user.client.rpc.AsyncCallback;

public class LoginPanel extends FormPanel {

	private TextField<String> email;
	private TextField<String> password;
	private Button signin;
	private FormButtonBinding binding;
	
	FantaBestConstants constants = Registry.get(RegistryKeys.CONSTANTS);
	FantaBestMessages messages = Registry.get(RegistryKeys.MESSAGES);

	public LoginPanel()	{
		initUi();
	}
	
	private void initUi()	{
		this.setHeading(constants.login());
		this.setWidth(300);
		this.setFrame(true);
		this.setCollapsible(false);
		this.setLabelAlign(LabelAlign.RIGHT);
		this.setLabelWidth(90);
		this.setFieldWidth(160);
		this.setPadding(0);
		this.setButtonAlign(HorizontalAlignment.CENTER);
		
		email = new TextField<String>();
		email.setFieldLabel(constants.email());
		email.setAllowBlank(false);
		email.setAutoValidate(true);
		email.setRegex(".+@.+\\.[a-z]+");
		email.getMessages().setRegexText("Invalid email.");
		email.setMaxLength(30);
		email.getMessages().setBlankText(messages.required(constants.email()));
		
		password = new TextField<String>();
		password.setFieldLabel(constants.password());
		password.setAllowBlank(false);
		password.setAutoValidate(true);
		password.setPassword(true);
		password.setMaxLength(30);
		password.getMessages().setBlankText(messages.required(constants.password()));
		
		signin = new Button(constants.login());
		signin.addSelectionListener(new SelectionListener<ButtonEvent>() {
			public void componentSelected(ButtonEvent ce) {
				LoginServiceAsync loginService = Registry.get(RegistryKeys.LOGIN_SERVICE);
				loginService.login(email.getValue(), password.getValue(), new AsyncCallback<Manager>() {
					public void onSuccess(Manager result) {
						BeanModel beanModel = null;
						if(result!=null)	{
							BeanModelFactory factory = BeanModelLookup.get().getFactory(Manager.class);
							beanModel = factory.createModel(result);
						}
						Dispatcher.get().dispatch(AppEvents.Login, beanModel);
					}
					public void onFailure(Throwable caught) {
						MessageBox.alert("Errore", caught.getMessage(), null);
					}
				});
			}
		});
		
		this.add(email);
		this.add(password);
		this.addButton(signin);
		
		binding = new FormButtonBinding(this);
		binding.addButton(signin);
	}
	
	
}
